package com.doge.exception.handler;

import com.doge.bean.base.BaseRespVo;
import com.doge.bean.pojo.log.Log;
import com.doge.exception.*;
import com.doge.mapper.LogMapper;
import com.doge.util.auth.GetSubject;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.Date;

/**
 * @program: doge
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-30 11:02
 **/

@RestControllerAdvice
public class MarketExceptionHandler {

    @ExceptionHandler(FileNotFound602Exception.class)
    public BaseRespVo fileNotFoundExceptionHander(Exception ex){
        // 文件不存在异常，管理员已经存在异常
        return BaseRespVo.error(602,ex.getMessage());
    }

    @ExceptionHandler(File402Exception.class)
    public BaseRespVo file402Exception(Exception ex){
        // 文件类型 和 文件名过长 异常
        return BaseRespVo.error(402,ex.getMessage());
    }

    @ExceptionHandler(SizeLimitExceededException.class)
    public BaseRespVo fileOverSizeException(Exception ex){
        // 文件过大异常
        return BaseRespVo.error(402,"上传文件必须在10MB以内！");
    }

    @ExceptionHandler(Param602Exception.class)
    public BaseRespVo paramExceptionHander(Exception ex){
        // 管理员已经存在异常
        return BaseRespVo.error(602,ex.getMessage());
    }
    @ExceptionHandler(Param402Exception.class)
    public BaseRespVo parameter402ExceptionHandler(Param402Exception ex){
        // 参数错误异常
        return BaseRespVo.error(402,ex.getMessage());
    }

    @ExceptionHandler(Delete502Exception.class)
    public BaseRespVo deleteExceptionHander(Exception ex){
        // 任何删除失败抛出该异常
        return BaseRespVo.error(502,ex.getMessage());
    }
    @ExceptionHandler(Param401Exception.class)
    public BaseRespVo parameter401ExceptionHandler(Exception ex){
        // 参数错误异常
        return BaseRespVo.error(401,ex.getMessage());
    }

    @ExceptionHandler(Param621Exception.class)
    public BaseRespVo parameter621ExceptionHandler(Exception ex){
        // 订单退款失败
        return BaseRespVo.error(621,ex.getMessage());
    }

    @ExceptionHandler(Param702Exception.class)
    public BaseRespVo parameter702ExceptionHandler(Exception ex){
        // 订单退款失败
        return BaseRespVo.error(702,ex.getMessage());
    }

    @ExceptionHandler({InvalidFormatException.class, MethodArgumentTypeMismatchException.class})
    public BaseRespVo parameterJacksonInvalidFormatException(Exception ex){
        // json转换,及get方法接收 参数异常
        return BaseRespVo.error(402,"参数值不对");
    }

    @ExceptionHandler(ServerException.class)
    public BaseRespVo serverException(Exception ex){
        // 数据库异常(有重复的用户名)，服务器参数异常
        return BaseRespVo.error(500,ex.getMessage());
    }

    /*
    * 未登录状态下 访问需要认证的资源，抛出该异常
    * */
    @ExceptionHandler(Login501Exception.class)
    public BaseRespVo loginException(Login501Exception ex){
        return BaseRespVo.error(501,ex.getMessage());
    }

    /*
    * credentail为null的处理
    * */
    @ExceptionHandler(UnknownAccountException.class)
    public BaseRespVo unknownException(){
        return BaseRespVo.error(605,"当前无此用户！");
    }

    /*
    * 密码不匹配
    * */
    @Autowired
    LogMapper mapper;
    @ExceptionHandler(AuthenticationException.class)
    public BaseRespVo incorrectCredentialsException(){
        loginLog("密码错误");
        return BaseRespVo.error(605,"密码错误！");
    }

    /*
     * 无权限处理
     * */
    @ExceptionHandler(AuthorizationException.class)
    public BaseRespVo authorizationException(){
        // 记录登录失败操作
        loginLog("无操作权限");
        return BaseRespVo.error(506,"无操作权限！");
    }

    private void loginLog(String result) {
        // 记录登录失败操作
        Log log = new Log();
        // 获取 管理员 和 ip
        log.setAdmin("匿名用户");
        log.setIp(GetSubject.getIp());
        log.setAddTime(new Date());
        log.setUpdateTime(new Date());
        log.setDeleted(false);
        log.setStatus(false);
        log.setType(1);
        log.setAction("登录");
        log.setResult(result);
        // 记录操作
        mapper.insertSelective(log);
    }

}
